/**
 * Created by zander on 2017/12/13.
 */
function getauthType() {
    var select = document.getElementById("select");
    var str = [];
    for (i = 0; i < select.length; i++) {
        if (select.options[i].selected) {
            str.push(select[i].value);
        }
    }
    return str
}

function getProject() {
    var a = document.getElementById("Project");
    var b = a.options[a.selectedIndex].value;
    return b;
}

function clearInfo() {
    var patrn1 = /^[0-9]*[1-9][0-9]*$/;
    var patrn = /^[0-9a-zA-Z]/;
    // var patrn = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)|(^\+?[1-9][0-9]*$)/;
    var inputPhone = $('#inputPhone').val();
    var inputId = $('#inputID').val();
    if (inputPhone == ""){
        alert("手机号为空");
        return false;
    }else if (inputId == ""){
        alert("身份证号为空");
        return false;
    }else if (!patrn1.test(inputPhone)){
        alert("手机号只能输入数字");
        return false;
    }else if (!patrn.test(inputId)){
        alert("身份证号格式错误");
        return false;
    }
    var localStorage = window.localStorage;
    var username = localStorage.getItem('name');
    var authType = getauthType();
    if (getauthType() == ""){
        alert("未选中任何授信项");
        return false;
    }
    document.getElementById('res').innerText = '';
    var project = getProject();
    var phone = document.getElementById('inputPhone').value;
    var identifier = document.getElementById('inputID').value;
    var status = "fail";
    sure(username, project, authType, phone, identifier, status);
}
function sure(username, project, authType, phone, identifier, status) {
    bootbox.confirm({
        size: "small",
        message: "是否确认清除用户数据?",
        callback: function (result) {
            if (result == true) {
                $.ajax({
                    type: "POST",
                    url: "/v1/modifyC2auth",
                    data: {
                        username: username,
                        project: project,
                        phone: phone,
                        identifier: identifier,
                        itemList: authType,
                        status: status
                    },
                    traditional: true,
                    dataType: "json",
                    beforeSend:function(){
                        $("body").mLoading("show");
                    },
                    success: function (ret) {
                        if (ret.status == "0000") {
                            var optionName = getListValue(authType);
                            var alertMsg = [];
                            for (x = 0; x < optionName.length; x++) {
                                alertMsg.push(optionName[x]);
                                alertMsg.push(ret.content[x]['message']);
                            }
                            $("body").mLoading("hide");
                            $("#res").append(alertMsg);
                        }
                    },
                    error: function() {
                        $("body").mLoading("hide");
                        $("#res").append("Error 500,请联系开发人员！");
                    }
                });
            }
        }
    })
}



function getListValue(authType) {
    var opts = $("#select");
    var str = [];
    for (var i = 0; i < authType.length; i++) {
        var se = "option[value='" + authType[i] + "']";
        str.push(opts.find(se).text());
    }
    return str
}
